package com.itnoob.service.impl.decorator;

import com.itnoob.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author zhoujun 定义一个具体的装饰者类,它表示日志功能
 * @date 2024/12/7 10:21
 */
@Service("logUserService")
public class LogUserServiceDecorator extends UserServiceDecorator{

    //通过构造方法注入被装饰的service对象,并使用@Autowired注解让spring自动为我们找到合适的bean对象
    @Autowired
    public LogUserServiceDecorator(UserService userService) {
        super(userService);
    }

    @Override
    public String getUserName(int id) {

        //记录请求开始时间,模拟打印日志
        long startTime = System.currentTimeMillis();

        String userName = userService.getUserName(id);

        long endTime = System.currentTimeMillis();

        System.out.println("请求时间:" + (endTime - startTime)+"毫秒");

        return userName;
    }
}
